var my_tweets = new Array(15);
var tweet_index = 0;
$(document).ready(function(){
	jQuery.each(jQuery.browser, function(i, val) {
		if(i == "mozilla"){
			/*$("#photo").animate({
				left: '20%',
				top: '11%'
			});*/
		}
	});
	var ua = navigator.userAgent;
	var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);
	var isiPad = navigator.userAgent.match(/iPad/i) != null;
	if(isiPad){
		$("#photo").animate({
			left: '3%',
			top: '12%'
		});
	}
	if(document.domain != "miqdadalik.ueuo.com" && document.domain != "localhost" && document.domain != "blog.miqdadalik.com"){document.body.innerHTML = ""; return;};
	$("#about").slideDown();
	$("#tweets_block").fadeIn();
	$("#menu li a").click(function(e){
		var id = e.target.title;
		if(id == "blog"){
			$("#tweets_block").hide();
			return;
		}
		else{
			$("#tweets_block").hide();
			$("#menu li").removeClass("current");
			$(e.target).parent().addClass("current");
			$(".content").hide();
			$("#"+id).slideDown();
			$("#tweets_block").fadeIn();
			if(id == "contact" || id == "education"){
				$("#tweets_block").animate({
					'margin-top' : '55px'
				})
				$("#tweets_block .sidebar").animate({
					width: '93%'
				});
				$("blockquote p").css({
					'padding-right': '10px'
				});
			}
			else{
				$("#tweets_block .sidebar").animate({
					width: '63%'
				});
				$("#tweets_block").animate({
					'margin-top' : '0px'
				})
			}
		}
	});
	$("a.move_link").click(function(e){
		var id = e.target.title;
		$(".content").hide();
		$("#"+id).slideDown();
	})
	
	$.getJSON('config.php?option=get_tweet', function(tweets) {
		if(tweets){
			if(tweets['tweets']){
				$("#tweets_block").fadeIn();
				$("#tweet_body").html(tweets['tweets'][0]['text']);
				$("#tweet_date").html(tweets['tweets'][0]['time']);
				tweet_index++;
				var refreshId = setInterval(function(){
					$("#tweet_body").hide();
					$("#tweet_body").fadeIn();
					$("#tweet_body").html(tweets['tweets'][tweet_index]['text']);
					$("#tweet_date").html(tweets['tweets'][tweet_index]['time']);
					tweet_index++;
					if(tweet_index == 10){
						tweet_index = 0;
					}
				},6000);
			}
		}
	});
	
	$.getJSON('config.php', function(data) {
		if(data){
			if(data['blog']['title'][0]){
				$("#blog_post_clip").fadeIn();
				$("#blog_post_block").fadeIn();
				$("#last_blog_post_title").html(data['blog']['title'][0]);
				$("#last_blog_post_time").html(data['blog']['time']);
				$("#last_blog_post_cont").html(data['blog']['desc']+" <a href='"+data['blog']['link'][0]+"' target='_blank'>read more</a>");
				$("#last_blog_post_title").hide();
				$("#last_blog_post_time").hide();
				$("#last_blog_post_cont").hide();
				
				$("#last_blog_post_title").fadeIn();
				$("#last_blog_post_time").fadeIn();
				$("#last_blog_post_cont").fadeIn();
			}
		}
	});
	$.getJSON('config.php?option=quotes', function(quotes) {
		if(quotes){
			if(quotes['author']){
				$("#quote_of_the_day").fadeIn();
				$("#paperClipLeftQuote").fadeIn();
				$("#paperClipRightQuote").fadeIn();
				$("#quote_of_the_day blockquote p").html(quotes['quote']);
				$("#quote_author").html(quotes['author']);
				$("#quote_of_the_day blockquote p").shuffleLetters();
			}
		}
	});
	$.getJSON('config.php?option=save_user', function(data) {
	});
	
	$("#contact_submitted").click(function(){
		$("#contact_message").fadeIn();
		var name = $.trim($("#your_name").val());
		var email = $.trim($("#your_email").val());
		var message = $.trim($("#your_enquiry").val());
		if(name == '' || email == '' || message == ''){
			$("#contact_message").html("Please fill the form. :)");
			$("#contact_message").removeClass("success").addClass("error");
			$("#contact_message").focus();
			return;
		}
		if(!validateEmail(email)){
			$("#contact_message").html("You have entered a wrong email :(");
			$("#contact_message").removeClass("success").addClass("error");
			$("#contact_message").focus();
			return;
		}
		
		$.getJSON('config.php?option=send_contact',{user_name: name, user_email: email, user_msg: message }, function(response) {
			if(response){
				if(response['error'] == true){
					$("#contact_message").removeClass("success").addClass("error");
					$("#contact_message").focus();
				}
				else{
					$("#contact_message").removeClass("error").addClass("success");
					$("#contact_message").focus();
				}
				$("#contact_message").html(response['text']);
				$("#contact_message").focus();
				$("#your_name").val("");
				$("#your_email").val("");
				$("#your_enquiry").val("");
			}
		});
		
		setTimeout(function(){
			$("#contact_message").removeClass("error");
			$("#contact_message").removeClass("success");
			$("#contact_message").fadeOut();
		},2000);
	});
	$(document).snowfall({shadow : true, round : true,  minSize: 3, maxSize:5, flakeCount : 50});
	$("#my_name").shuffleLetters();
	$("#my_slogan").shuffleLetters();
	
	$("#change_my_dress").click(function(){
		$.getJSON('config.php?option=change_dress', function(data) {
			document.location.reload();
		})
	});
});
// On window load. This waits until images have loaded which is essential
$(window).load(function(){
	
	// Fade in images so there isn't a color "pop" document load and then on window load
	$("#my_photo").fadeIn(500);
	
	// clone image
	$('#my_photo').each(function(){
		var el = $(this);
		el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
			var el = $(this);
			el.parent().css({"width":this.width,"height":this.height});
			el.dequeue();
		});
		this.src = grayscale(this.src);
	});
	
	// Fade image 
	$('#my_photo').mouseover(function(){
		$(this).parent().find('img:first').stop().animate({opacity:1}, 1000);
	})
	$('.img_grayscale').mouseout(function(){
		$(this).stop().animate({opacity:0}, 1000);
	});		
});

// Grayscale w canvas method
function grayscale(src){
	var canvas = document.createElement('canvas');
	var ctx = canvas.getContext('2d');
	var imgObj = new Image();
	imgObj.src = src;
	canvas.width = imgObj.width;
	canvas.height = imgObj.height; 
	ctx.drawImage(imgObj, 0, 0); 
	var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
	for(var y = 0; y < imgPixels.height; y++){
		for(var x = 0; x < imgPixels.width; x++){
			var i = (y * 4) * imgPixels.width + x * 4;
			var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
			imgPixels.data[i] = avg; 
			imgPixels.data[i + 1] = avg; 
			imgPixels.data[i + 2] = avg;
		}
	}
	ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
	return canvas.toDataURL();
}
function validateEmail($email){
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	return emailReg.test( $email );
}

